<?php

namespace App\Admin\Controllers;

use App\Miya\Models\Info;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DelayTaskController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '延时展示任务';

    public function __construct()
    {
        $this->description['index'] = '手动开启延时并设置延时展示时间的信息不在此列!';
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Info());
        $grid->model()->where('push_delay_task', 1)
            ->with(['module', 'classify'])->withoutGlobalScope('id')
            ->orderBy('delayed_at');

        $grid->column('id', '序号');
        $grid->column('module_id', '模块')->display(function () {
            return $this->module->name;
        })->label();
        $grid->column('classify_id', '分类')->display(function () {
            return $this->classify->name;
        })->label();
        $grid->column('name', '信息')->label();
        $grid->column('keyword_id', '关键词')->display(function () {
            return $this->keyword->keyword ?? '';
        });
        $grid->column('delayed_at', '延时展示时间');
        $grid->column('created_at', '创建时间')->display(function () {
            return $this->created_at->format('Y-m-d H:i:s');
        });
        $grid->column('is_show', '是否展示')->display(function () {
            if ($this->delayed_at >= now()) {
                return '延时中';
            } else {
                return '已显示';
            }
        })->label(['延时中' => 'success', '已显示' => 'info']);

        $grid->disableActions();
        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Info());

        $form->radio('push_delay_task', '开始延时任务')
            ->help('开始延时任务,会自动根据当前的任务量对此信息进行延时展示')
            ->default(2)->options(Info::$states);
        $form->radio('is_delayed', '是否开启延时展示')->default(2)->options(Info::$states);
        $form->datetime('delayed_at', '延时展示时间');

        return $form;
    }
}
